package com.obas.business.analogy.view {
	import com.ease.component.managers.ConfirmHelper;
	import com.ease.component.managers.ErrorManager;
	import com.ease.core.events.ViewOperationEvent;
	import com.ease.framework.CommandCompletionEvent;
	import com.obas.business.component.tree.treeclass.ErrorNode;
	import com.obas.business.component.tree.treeclass.LoadingNode;
	import com.obas.business.component.tree.treeclass.Node;
	import com.obas.common.constant.NetworkLibConstant;
	import com.obas.common.event.CommonOperationEvent;
	import com.obas.common.event.DataRefreshEvent;
	import com.obas.common.util.ListDataUtil;
	import com.obas.common.util.ResourceUtil;
	import com.obas.model.mature.NetworkBusiness;
	
	import flash.events.ContextMenuEvent;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Tree;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	[Event(name="{com.ease.core.events.ViewOperationEvent.REQUEST_ID}", type="com.ease.core.events.ViewOperationEvent")]
	[Event(name="{com.obas.common.event.CommonOperationEvent.REQUEST_ID}", type="com.obas.common.event.CommonOperationEvent")]
	
	public class UserExperienceExplorerMediator extends EventDispatcher {
		private var listUtil:ListDataUtil = new ListDataUtil(keyFunction);
		
		public var _view:UserExperienceExplorerView;
		
		[InjectableView]
		public function get view():UserExperienceExplorerView {
			return _view;
		}
		
		public function set view(value:UserExperienceExplorerView):void {
			_view = value;
			init();
		}
		
		private function init():void {
			
//			var childNode:Node = null;
//			var leafNode:Node = null;
			
//			childNode = view.navTree.nodeFactory.createNode({name:"网络质量",code:"NetworkQuality"});
//			childNode.type = NetworkLibConstant.ITEMTYPE_OBJECT;			
//			view.treeNodes.addItem(childNode);
//			
//			childNode = view.navTree.nodeFactory.createNode({name:"业务质量",code:"BusinessQuality"});
//			childNode.type = NetworkLibConstant.ITEMTYPE_OBJECT;			
//			view.treeNodes.addItem(childNode);
//			
//			childNode = view.navTree.nodeFactory.createNode({name:"用户感知",code:"UserQuality"});
//			childNode.type = NetworkLibConstant.ITEMTYPE_OBJECT;
//			view.treeNodes.addItem(childNode);
			
//			
//			
//			listUtil.addOrUpdateItems(childNode.children.source, [leafNode], true);
//			childNode.children.refresh();
			
//			view.addEventListener(ProjectEmptyMenu.CREATE_PROJECT, onCreateProject);
//			view.addEventListener(ProjectMenu.DELETE_PROJECT, onDeleteProject);
//			view.addEventListener(ProjectMenu.MODIFY_PROJECT, onModifyProject);
//			
//			view.addEventListener(ProjectMenu.CREATE_SCENARIO, onCreateScenario);
//			view.addEventListener(ScenarioMenu.MODIFY_SCENARIO, onModifyScenario);
//			view.addEventListener(ScenarioMenu.DELETE_SCENARIO, onDeleteScenario);
//			
//			view.addEventListener(ScenarioMenu.CREATE_CASE, onCreateCase);
//			view.addEventListener(CaseMenu.MODIFY_CASE, onModifyCase);
//			view.addEventListener(CaseMenu.DELETE_CASE, onDeleteCase);
//			
//			view.addEventListener(CaseMenu.CREATE_FUNCTION, onCreateFunction);
//			view.addEventListener(FunctionMenu.MODIFY_FUNCTION, onModifyFunction);
//			view.addEventListener(FunctionMenu.DELETE_FUNCTION, onDeleteFunction);
//			
//			view.addEventListener(FunctionMenu.CREATE_SCRIPT, onCreateScript);
			
//			view.navTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDoubleClick);
			view.explorerTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onExplorerTreeDoubleClick);
			
			view.configrationTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onConfigrationTreeItemDoubleClick);
//			dispatchEvent(new CommonOperationEvent(null));
			//增加右键菜单
			/*
			var projectContextMenu:ContextMenu = new ContextMenu();
			var newProjectContextMenuItem:ContextMenuItem = new ContextMenuItem("新建项目");
			projectContextMenu.customItems.push(newProjectContextMenuItem);
			newProjectContextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, newProject);
			this._view.contextMenu = projectContextMenu;
			*/
		}
		
		
		
		private function onConfigrationTreeItemDoubleClick(e:ListEvent):void {
			var item:Object = Tree(e.currentTarget).selectedItem;
			if (view.configrationTree.dataDescriptor.isBranch(item)) {
				view.configrationTree.expandItem(item, !view.configrationTree.isItemOpen(item), true);
			} else {
				
			}
		}
		
		
//		private function newProject(evt:ContextMenuEvent):void {
//			var dialog:ProjectNewView = PopUpManager.createPopUp(view.root, ProjectNewView, true) as ProjectNewView;
//			PopUpManager.centerPopUp(dialog);
//		}
		
		private function keyFunction(item:*):String {
			return item.entity.id + "";
		}
		
		[ResponseHandler("{com.obas.common.event.CommonOperationEvent.RETURN_ID}")]
		public function onCommonOperationEvent(event:CommandCompletionEvent):void {
			if (event.error) {
				ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
				return ;
			}
			
			var request:CommonOperationEvent = event.request as CommonOperationEvent;
			if (request.actionType == CommonOperationEvent.GET_NETWORKBUSINESS) {
				var results:ArrayCollection = event.result as ArrayCollection;
//				for each (var project:NetworkBusiness in results) {
//					var childNode:Node = view.navTree.nodeFactory.createNode(project);
//					childNode.type = NetworkLibConstant.ITEMTYPE_NETWORKBUSINESS;
//					view.treeNodes.addItem(childNode);
//				}
			}
			else if (request.actionType == CommonOperationEvent.DELETE_CASE ||
				request.actionType == CommonOperationEvent.DELETE_FUNCTION ||
				request.actionType == CommonOperationEvent.DELETE_PROJECT ||
				request.actionType == CommonOperationEvent.DELETE_SCENARIO||
				request.actionType == CommonOperationEvent.DELETE_SCRIPT) {
				var node:Node = request.spec[1];
				var parentNode:Node = node.parentNode;
				if (parentNode) {
					parentNode.children.removeItemAt(parentNode.children.getItemIndex(node));
					parentNode.children.refresh();
				}
				else {
					view.treeNodes.removeItemAt(view.treeNodes.getItemIndex(node));
				}
//				view.navTree.invalidateDisplayList();
			}
		}
		
		
		private function onExplorerTreeDoubleClick(e:ListEvent):void {
			var item:Object = Tree(e.currentTarget).selectedItem;
			
			if (view.configrationTree.dataDescriptor.isBranch(item)) {
				view.configrationTree.expandItem(item, !view.configrationTree.isItemOpen(item), true);
				return;
			}
			
			
			var code:String = item.@code;
			if(null!=code) {
				
				var param:Object = null;
				if ("UserBusinessExperienceUserGridView"==code) {
					param = new Object();
					param.targetView = "userBusinessExperienceUserGridView";
					param.className = "com.obas.business.analogy.view.UserBusinessExperienceUserGridView";
					param.createNew = "false";
					param.autoOpen = "true";
					//				param.entity = selectedNode.entity;
					dispatchEvent(new ViewOperationEvent([param.targetView], param));
				} else if ("UserBusinessExperienceIndicatorGridView"==code) {
					param = new Object();
					param.targetView = "userBusinessExperienceIndicatorGridView";
					param.className = "com.obas.business.analogy.view.UserBusinessExperienceIndicatorGridView";
					param.createNew = "false";
					param.autoOpen = "true";
					//				param.entity = selectedNode.entity;
					dispatchEvent(new ViewOperationEvent([param.targetView], param));
				} else if ("UserNetworkExperienceUserGridView"==code) {
					param = new Object();
					param.targetView = "userNetworkExperienceUserGridView";
					param.className = "com.obas.business.analogy.view.UserNetworkExperienceUserGridView";
					param.createNew = "false";
					param.autoOpen = "true";
					//				param.entity = selectedNode.entity;
					dispatchEvent(new ViewOperationEvent([param.targetView], param));
				} else if ("UserNetworkExperienceIndicatorGridView"==code) {
					param = new Object();
					param.targetView = "userNetworkExperienceIndicatorGridView";
					param.className = "com.obas.business.analogy.view.UserNetworkExperienceIndicatorGridView";
					param.createNew = "false";
					param.autoOpen = "true";
					//				param.entity = selectedNode.entity;
					dispatchEvent(new ViewOperationEvent([param.targetView], param));
				}
			}
			
			
			
		}
		
		private function onDoubleClick(e:*):void {
//			var selectedNode:Node = view.navTree.selectedItem as Node;
//			var param:Object = null;
//			if (selectedNode.type == NetworkLibConstant.ITEMTYPE_OBJECT) {
//				var object:Object = selectedNode.entity;
//				if("USERAPP"==object.code) {
//					param = new Object();
//					param.targetView = "dimensionUserAppGridView";
//					param.className = "com.obas.business.analogy.view.DimensionUserAppGridView";
//					param.createNew = "true";
//					param.autoOpen = "true";
//					param.entity = selectedNode.entity;
//					dispatchEvent(new ViewOperationEvent([param.targetView], param));
//				}				
//			}else
//			
//			
//			if (selectedNode.type == NetworkLibConstant.ITEMTYPE_SCRIPT) {
//				param= new Object();
//				param.targetView = "commonView";
//				param.className = "com.ease.itest.view.script.ScriptEditView";
//				param.createNew = "false";
//				param.autoOpen = "true";
//				param.entity = selectedNode.entity;
//				dispatchEvent(new ViewOperationEvent([param.targetView], param));
//			} else {
//				var paramOther:Object = new Object();
//				paramOther.targetView = "commonView";
//				paramOther.className = "com.ease.itest.view.EntityView";
//				paramOther.createNew = "false";
//				paramOther.autoOpen = "true";
//				paramOther.model = selectedNode.entity;
//				dispatchEvent(new ViewOperationEvent([paramOther.targetView], paramOther));
//			}
		}
		
		
		private function deleteItems(items:ArrayCollection):void {
		}
		
		public function getNodeType(item:*):String {
			
			return view.getNodeType(item);
		}
		
	}
}